package com.kiln.juxingqs.ui.kchart

import android.content.Intent
import android.content.res.Configuration
import android.view.View
import android.widget.LinearLayout
import com.github.tifezh.kchartlib.chart.formatter.DateFormatter
import com.github.tifezh.kchartlib.chart.view.BaseKChartView
import com.github.tifezh.kchartlib.chart.view.KChartView
import com.kiln.juxingqs.BaseMiddleActivity
import com.kiln.juxingqs.R
import com.kiln.juxingqs.adapter.chart.KChartAdapter
import com.kiln.juxingqs.databinding.ActivityKlineDayBinding
import com.kiln.juxingqs.ui.SyDetailActivity
import com.kiln.juxingqs.ui.vm.KLineModel

/**
 * Date: 2023/1/6 11:50
 * Description:
 */
class KlineDayActivity : BaseMiddleActivity<KLineModel, ActivityKlineDayBinding>() {

    private var goodsId: String? = null
    private var mAdapter: KChartAdapter? = null

    lateinit var mKChartView: KChartView
    lateinit var mLlStatus: LinearLayout

    override fun getLayout(): Int {
        return R.layout.activity_kline_day
    }

    override fun initViewModel(): KLineModel = createViewModel()

    override fun initView() {
        super.initView()
        binding.event = this
        binding.vm = viewModel

        goodsId = intent.getStringExtra("goodsId")!!
        initViewKLine()
//        viewModel.holdCall(goodsId)

    }

    override fun observerUI() {
        super.observerUI()
//        viewModel.callkLine.observe(this, {
//            it?.let {
//                if (it.isSuccess) {
////                    initDataKline(it.data?.day_k_list as MutableList<DayK.KLineEntity2?>)
//
//                    viewModel.callkLine.value = null
////                    val list = it.data?.order_list as MutableList<ZrOrder.Order?>
////                    if (viewModel.pageOne.value == 1) {
////                        adapter.submitList(list)
////                    } else
////                        adapter.appendList(list)
////                    if (list != null && list.size > 0) {
////                        if (list.size < 20) {
////                            adapter.hasMoreState.value = false
////                        }
////                        binding.emptyLayout.visibility = View.GONE
////                    } else {
////                        if (adapter.itemCount == 0)
////                            binding.emptyLayout.errorType = EmptyLayout.NODATA
////                        adapter.hasMoreState.value = false
////                    }
//                }
////                else {
////                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
////                }
//
//            }
//        })

    }

    private fun initViewKLine() {
        mKChartView = binding.kchartView
        mLlStatus = binding.llStatus

        mAdapter = KChartAdapter()
        mKChartView.setAdapter(mAdapter)
        mKChartView.setDateTimeFormatter(DateFormatter())
        mKChartView.setGridRows(4)
        mKChartView.setGridColumns(4)
        mKChartView.setOnSelectedChangedListener(BaseKChartView.OnSelectedChangedListener { view, point, index ->
//            val data: DayK.KLineEntity2 = point as DayK.KLineEntity2
//            Log.i("onSelectedChanged", "index:" + index + " closePrice:" + data.getClosePrice())
        })

//        binding.refreshLayout.setOnRefreshListener {
//            viewModel.pageOne.value = 0
//            adapter.hasMoreState.value = true
//            viewModel.holdCall(goods_id)
//        }
//        viewModel.isOneLoading.observe(this, {
//            if (!it) {
//                binding.refreshLayout.isRefreshing = false
//            }
//        })
    }

//    private fun initDataKline(data: MutableList<DayK.KLineEntity2?>) {
////        val data: List<KLineEntity> = DataRequest.getALL(this)
//        mKChartView.showLoading()
////        Thread {
////            runOnUiThread {
////                mAdapter?.addFooterData(data)
////                mKChartView.startAnimation()
////                mKChartView.refreshEnd()
////            }
////        }.start()
//    }


    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        if (this.resources.configuration.orientation === Configuration.ORIENTATION_LANDSCAPE) {
            mLlStatus.setVisibility(View.GONE)
            mKChartView.setGridRows(3)
            mKChartView.setGridColumns(8)
        } else if (this.resources.configuration.orientation === Configuration.ORIENTATION_PORTRAIT) {
            mLlStatus.setVisibility(View.VISIBLE)
            mKChartView.setGridRows(4)
            mKChartView.setGridColumns(4)
        }
    }

    fun goSyDetail() {
        startActivity(Intent(this, SyDetailActivity::class.java).apply {
            putExtra("goodsId", "${goodsId}")
        })
    }

}